﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using Alogient.Cameleon.Sdk.Items;

namespace Alogient.Cameleon.Sdk.UrlRewriting
{
    public sealed class Utils
    {
        public static string ConstructUrl(CMPage p_cmp)
        {
            string prefix = "/";

            if (!string.IsNullOrEmpty(HttpContext.Current.Request.ApplicationPath))
            {
                prefix = VirtualPathUtility.ToAbsolute("~/");
            }

            if (CMSettings.UrlRewritingEnabled)
            {
                return string.Format("{0}{1}", prefix, p_cmp.RewrittenUrl);
            }

            return string.Format("{0}{1}", prefix, p_cmp.PageUrl);
        }

        public static String RemoveDiacritics(String s)
        {
            String normalizedString = s.Normalize(NormalizationForm.FormD);
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < normalizedString.Length; i++)
            {
                Char c = normalizedString[i];
                if (CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.DecimalDigitNumber
                    || CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.LowercaseLetter
                    || CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.UppercaseLetter
                    || c == '/' || c == '-')
                {
                    stringBuilder.Append(c);
                }
            }

            return stringBuilder.ToString();
        }
    }
}
